<div class="clear"></div>
<center>
	
	<form id="signupForm" name="signupForm"  method="post" class="form-horizontal" role="form">
	<div class="form-group">
		<label class="control-label col-sm-3"><strong>User Name:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="uname" value="<? if(isset($this->user_details->username)){echo $this->user_details->username;}?>" name="uname" class="form-control" placeholder="Full Name"/>&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroruname"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Company name:</strong></label>
		<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="cname" name="cname" class="form-control" placeholder="Company Name" value="<? if(isset($this->user_details->company_name)){echo $this->user_details->company_name;}?>" />&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcname"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Company contact number:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="ccnumber" name="ccnumber" class="form-control" placeholder="Company Contact Number" value="<? if(isset($this->user_details->company_contact_number)){echo $this->user_details->company_contact_number;}?>"/>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorccnumber"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Email:</strong></label>
			<div class="col-sm-9">
			<input readonly="readonly" class="input-btn usr-name" type="text" id="email" name="email"  class="form-control" placeholder="Email" value="<? if(isset($this->user_details->email)){echo $this->user_details->email;}?>"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroremail1"></span><span style="color:green" id="erroremail2"></span><span style="color:red" id="erroremail"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Phone Number:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="pnumber" name="pnumber" class="form-control" placeholder="Email" value="<? if(isset($this->user_details->phone)){echo $this->user_details->phone;}?>"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorpnumber"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Country Name:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="country" id="country">
				<option value="">-Select a country</option>	
				<?php if(isset($this->countries) && count($this->countries)>0){foreach($this->countries as $countries){ ?>
					<? if($this->user_details->country==$countries->country_id){?>
						<option value="<? echo $countries->country_id;?>" selected="selected"><? echo $countries->nicename;?></option>
					<?}else{?>
						<option value="<? echo $countries->country_id;?>"><? echo $countries->nicename;?></option>
				<?}}}?>				
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcountry"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>State Name:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="state" id="state">
				<option value="">-Select a state</option>
				<?php if(isset($this->states) && count($this->states)>0){foreach($this->states as $states){ ?>
					<? if($this->user_details->state==$states->state_id){?>
						<option value="<? echo $states->state_id;?>" selected="selected"><? echo $states->state_name;?></option>
					<?}else{?>
						<option value="<? echo $states->state_id;?>"><? echo $states->state_name;?></option>
				<?}}}?>						
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorstate"></span>
		</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>City:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="city" name="city" class="form-control" value="<? if(isset($this->user_details->city)){echo $this->user_details->city;}?>" placeholder="Please enter city"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorcity"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Address:</strong></label>
			<div class="col-sm-9">
			<textarea class="input-btn usr-name form-control" id="address" name="address" ><? if(isset($this->user_details->address)){echo $this->user_details->address;}?></textarea>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="erroraddress"></span>
		</div>
		
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Pincode Number:</strong></label>
			<div class="col-sm-9">
			<input class="input-btn usr-name" type="text" id="pcode" name="pcode" class="form-control" placeholder="Please enter pincode" value="<? if(isset($this->user_details->pincode)){echo $this->user_details->pincode;}?>"/>
			&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorpcode"></span>
			</div>
		</div>
		<div class="form-group">
		<label class="control-label col-sm-3"><strong>Type of user:</strong></label>
			<div class="col-sm-9">
			<select class="grid_6 column omega form-control" name="userr_type" id="userr_type">
				<option value="">-Select a user type</option>
				<? if(isset($this->user_details->type_of_user) && $this->user_details->type_of_user=='admin'){?>
					<option value="admin" selected="selected">Admin</option>										
					<option value="user">User/Company</option>	
				<?}else if(isset($this->user_details->type_of_user) && $this->user_details->type_of_user=='user'){?>
					<option value="admin">Admin</option>										
					<option value="user" selected="selected">User/Company</option>	
				<?}?>								
			</select>&nbsp;&nbsp;&nbsp;<span style="color:red" id="errorusertype"></span>
		</div>
		</div>
		<div class="registerBtnOver btn2Bg btn2b form-group">
		<a  id="reg_button" onclick="validate()" class="registerBtn btn btn-danger" href="javascript:void(0);" role="button">Update</a>
		<a class="close registerBtn btn btn-danger" id="cancel" href="javascript:void(0);" role="button">Back</a>
		<span style="display:none;" id="loadding"><img src="images/loader16.gif"></span></div>
		<span style="color:red" id="regAuthmail"></span>
		<input type="hidden" id="hiduserid" name="hiduserid" value="<? if(isset($this->user_details->user_id)){echo $this->user_details->user_id;}?>">
	</form>
</center>
<script type="text/javascript">
function validate(){
	var flag=true;	
	var emailcheck=$('#email').val();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var letters = /^[a-zA-Z ]*$/; 
	var numbers = /^[0-9]+$/;
	var username=$('#uname').val();
	var companyname=$('#cname').val();
	var companycontactnumber=$('#ccnumber').val();
	var pnumber=$('#pnumber').val();
	var country=$('#country').val();
	var state=$('#state').val();
	var city=$('#city').val();
	var address=$('#address').val();
	var pincode=$('#pcode').val();
	var userr_type=$('#userr_type').val();
	
	if(username==""){
		$('#erroruname').html('Required');	
		flag=false;
	}else if(!username.match(letters)){
		$('#erroruname').html('');
		$("#erroruname").html("Characters only");
		flag=false;
	}else{
		$('#errorfname').html('');
	}
	if(companycontactnumber!=""){
	 if(!companycontactnumber.match(numbers)){
	    $('#errorccnumber').html('numbers only');
		flag=false;
	}else if(companycontactnumber.length<10 || companycontactnumber.length>10 ){
		$('#errorccnumber').html('valid phonenumber');
		flag=false;
	}}else{
		$('#errorccnumber').html('');
	}
	if($('#email').val()==""){
		$('#erroremail1').html('');
		$('#erroremail').html('*Email Required');
		flag=false;
	}else if(checkEmail(emailcheck)==false){
		$('#erroremail1').html('');
		$('#erroremail2').html('');
		$('#erroremail').html('*Email Format is not correct');	
		flag=false;
	}else{
		$('#erroremail').html('');
	}	
	if(pnumber==""){
		$('#errorpnumber').html('Required');
		flag=false;
	}else if(!pnumber.match(numbers)){
		$('#errorpnumber').html('numbers only');
		flag=false;
	}else if(pnumber.length<10 || pnumber.length>10 ){
		$('#errorpnumber').html('valid phonenumber');
		flag=false;
	}else{
		$('#errorpnumber').html('');
	}
	if(country==""){
		$('#errorcountry').html('Required');
		flag=false;
	}else{
		$('#errorcountry').html('');
	}
    if(state==""){
		$('#errorstate').html('Required');
		flag=false;
	}else{
		$('#errorstate').html('');
	}
	if(city==""){
		$('#errorcity').html('Required');
		flag=false;
    }else{
        $('#errorcity').html('');
	}
	if(address==""){
		$('#erroraddress').html('Required');
		flag=false;
	}else{
		 $('#erroraddress').html('');
	}
	if(pincode==""){
		$('#errorpcode').html('Required');	
		flag=false;
	}else if(!pincode.match(numbers)){
		$('#errorpcode').html('numbers only');	
		flag=false;
	}else{
		$('#errorpcod').html('');
	}
	if(userr_type==""){
		$('#errorusertype').html('Required');	
		flag=false;
	}else{
		$('#errorusertype').html('');	
	}
	if(flag==false){ 
		return false;
	}else{
		$('#loadding').show();	
		$.ajax({
			type:	'POST',
			url:    '/users/updateprofile',
			data:	$( "#signupForm" ).serialize(),
			success: function(data){
				$('#loadding').hide();	
				if(data.output=='Update_success'){
					var path="<?echo $_SESSION['user']['username'] .'-'.$_SESSION['user']['user_id'].'-'.$_SESSION['user']['user_type'];?>";
					window.location='/menu';
					alert();
				}else{
					alert('Updating not success');
				}
			}
		});
	}	
}
	
function checkEmail(emailStr) {
	if (emailStr.length == 0) {
		return true;
	}
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
	var atom=validChars + "+";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat) == null) {
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				return false;
			}
		}
		return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray == null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length < 2) ||(domArr[domArr.length-1].length > 3)) {
		return false;
	}
	if (len < 2) {
		return false;
	}
	return true;
}
$("#country").change(function(){ 		
	var countryid=$("#country").val();	
	$.ajax({
		type:	'POST',
		url:   	 '/users/get-states',
		data:	{countryid:countryid},
		success: function(data){
			if(data.output=='success'){ 				
				$("#state").html(data.statenames);
			}							
		}
	});
});
</script>






























